package com.ttbj.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;



public class CheckInSignUtil {

	/**
	 * 验证签名
	 * 
	 * @param characterEncoding
	 * @param packageParams
	 * @param API_KEY
	 * @return
	 */
	public static boolean isTenpaySign(String characterEncoding, SortedMap<Object, Object> packageParams,
			String API_KEY) {
		StringBuffer sb = new StringBuffer();
		Set es = packageParams.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			if (!"sign".equals(k) && null != v && !"".equals(v)) {
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("key=" + API_KEY);
		// 算出摘要
		String mysign = MD5Util.md5(sb.toString(), characterEncoding);
		String tenpaySign = ((String) packageParams.get("sign"));
		return tenpaySign.equals(mysign);
	}

	/**
	 * 生成签名的密钥
	 * 
	 * @param characterEncoding
	 * @param packageParams
	 * @param secretKey
	 * @return
	 */
	public static String paySign(String characterEncoding, LinkedHashMap<String,String> packageParams,String Key) {
		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String, String> entry : packageParams.entrySet()) {
			String k = entry.getKey();
			String v = entry.getValue();
			if ( !"version".equals(k) && null != v && !"".equals(v)) {
				sb.append(k + "=" + v + "&");
			}else if("version".equals(k) ){
				sb.append(k + "=" + v);
			}
		} 
		sb.append(Key);
		System.out.println("代签名的号码是----"+sb.toString());
		// 算出摘要
		String mysign = MD5Util.md5(sb.toString(), characterEncoding);
		System.out.println("已经签名的是----"+mysign);
		return mysign;
	}

	/**
	 * sign签名 使用md5加密
	 * 
	 * @param characterEncoding
	 * @param packageParams
	 * @param api_key
	 * @return
	 */
	public static String createSign(String characterEncoding, SortedMap<Object, Object> packageParams, String api_key) {
		StringBuffer sBuffer = new StringBuffer();
		Iterator<Map.Entry<Object, Object>> it = packageParams.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<Object, Object> entry = it.next();
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();
			if (null != val && !"".equals(val) && !"sign".equals(key) && !"key".equals(key)) {
				sBuffer.append(key + "=" + val + "&");
			}
		}
		sBuffer.append("key=" + api_key);
		String sign = MD5Util.md5(sBuffer.toString(), characterEncoding);
		return sign;
	}
	
	
	/**
	 * 将请求参数转换为xml格式的string
	 * 
	 * @param params
	 * @return
	 */
	public static String getRequestXml(SortedMap<Object, Object> params) {
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("<xml>");
		Iterator<Map.Entry<Object, Object>> it = params.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<Object, Object> entry = it.next();
			String key = (String) entry.getKey();
			String val = (String) entry.getValue();
			if ("attch".equalsIgnoreCase(key) || "body".equalsIgnoreCase(key) || "sign".equalsIgnoreCase(key)) {
				sBuffer.append("<" + key + ">" + "<![CDATA[" + val + "]]></" + key + ">");
			} else {
				sBuffer.append("<" + key + ">" + val + "</" + key + ">");
			}
		}
		sBuffer.append("</xml>");
		return sBuffer.toString();
	}

	/**
	 * 取出一个指定长度的正整数
	 * 
	 * @param length
	 * @return
	 */
	public static int bulidRandom(int length) {
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 1;
		}
		for (int i = 0; i < length; i++) {
			num = num * 10;
		}
		return (int) (random * num);
	}

	/**
	 * 获取当前时间
	 * 
	 * @return
	 */
	public static String getCurrTime() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}

}
